package com.gp.testonline.controller;

import com.gp.testonline.eo.ExampaperEOPage;
import com.gp.testonline.model.Exampaper;
import com.gp.testonline.service.ExampaperService;
import com.gp.testonline.tool.ResponseMessage;
import com.gp.testonline.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对外提供数据服务接口
 */
@Api(tags = "exampaper")
@RestController
public class ExampaperController {
    @Autowired
    private ExampaperService exampaperService;

    @ApiOperation(value = "试卷信息添加方法")
    @RequestMapping(value = "/insertExampaper",method = RequestMethod.GET)
    public ResponseMessage insert(Exampaper exampaper){
        int IsInsert=0;
        //传入数据的判断
        //(1) examanswer不能为空
        if (exampaper!=null){
            //(2) 编号不能为0
            if (exampaper.getEpid()!=0){
                //(3) 添加数据
                IsInsert=exampaperService.InsertExampaper(exampaper);
                if (IsInsert==1){
                    return Result.success("添加成功！");
                }else if (IsInsert==-1){
                    return Result.error("添加失败！");
                }else{
                    return Result.error("保存失败！");
                }
            }
        }
        return Result.error("保存失败！");
    }

    @ApiOperation(value = "试卷信息更新方法")
    @RequestMapping(value = "/updateExampaper",method = RequestMethod.GET)
    public ResponseMessage update(Exampaper exampaper){
        int IsUpdate=0;
        //传入数据的判断
        //(1) examanswer不能为空
        if (exampaper!=null){
            //(2) 编号不能为0
            if (exampaper.getEpid()!=0){
                //(3) 更新数据
                IsUpdate=exampaperService.InsertExampaper(exampaper);
                if (IsUpdate==1){
                    return Result.success("修改成功！");
                }else if (IsUpdate==-1){
                    return Result.error("修改失败！");
                }
            }
        }
        return Result.error("修改失败,id不能为0！");
    }

    @ApiOperation(value = "试卷删除方法")
    @RequestMapping(value = "/deleteExampaper",method = RequestMethod.GET)
    public ResponseMessage deleteExampaper(int epid){
        int isDelete=0;
        if (epid!=0){
            isDelete=exampaperService.DeleteExampaper(epid);
            if (isDelete>0){
                return Result.success("删除成功！");
            }else{
                return Result.error("删除失败！");
            }
        }else {
            return Result.error("删除的主键不能为0，请重新输入！");
        }
    }

    @ApiOperation(value = "根据条件获取试卷详细信息")
    @RequestMapping(value = "/getExampaper",produces = "application/json;charest=utf-8",method = RequestMethod.PUT)
    public ResponseMessage getExampaper(ExampaperEOPage exampaperEOPage) {
        Map<String, List> map = new HashMap<>();
        List<ExampaperEOPage> listExampaper = exampaperService.getExampaperAll(exampaperEOPage);
        map.put("list",listExampaper);
        return Result.success("200",map);
    }
}



